package com.zlt.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.*;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.InputStream;

/**
 * @author H
 * @date 2022/8/15 8:56
 **/
public class QnyUtil {
    
    private static final String ACCESS_KEY = "UTH_ulya24CBmj1ZBMMo14pMMplxHuIqOHu6UY9B";
    
    private static final String SECRET_KEY = "5H_LZyjKq9E_aMJhpyKIxP-dLBTvbqr4ETAzz8Sf";
    
    private static final String BUCKET_NAME = "zlt-zhaopin";
    
    private static final String BASE_PATH = "http://rgmsnjeju.hn-bkt.clouddn.com/";
    
    private static final String DOMAIN = "rgmsnjeju.hn-bkt.clouddn.com";
    
    /**
     * 上传文件到七牛云服务层
     *
     * @param reader   输入流
     * @param fileName 文件名（包括文件扩展名）
     */
        public static String upload(InputStream reader, String fileName) {
        String upToken;
        String path;
        
        try {
            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
            upToken = auth.uploadToken(BUCKET_NAME);
            
            // 上传的空间
            Configuration configuration = new Configuration(Region.region2());
            
            // 上传的管理工具
            UploadManager uploadManager = new UploadManager(configuration);
            
            Response response = uploadManager.put(reader, fileName, upToken, null, null);
            
            // 解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            
            // 返回访问地址
            path = BASE_PATH + fileName;
            return path;
        } catch (QiniuException ex) {
            Response r1 = ex.response;
            System.err.println(r1.toString());
            try {
                System.err.println(r1.bodyString());
            } catch (QiniuException ex2) {
                Response r2 = ex2.response;
                System.err.println(r2.toString());
            }
            return null;
        }
    }
    
    /**
     * 从七牛云服务器删除指定文件
     *
     * @param key 文件名（包括文件扩展名）
     */
    public static boolean delete(String key) {
        Configuration cfg = new Configuration(Region.region2());
        
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(BUCKET_NAME, key);
            return true;
        } catch (QiniuException ex) {
            // 如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
            return false;
        }
    }
    
    /**
     * 从七牛云服务器删除指定文件
     *
     * @param key 文件名（包括文件扩展名）
     */
    public static String download(String key) {
        DownloadUrl url = new DownloadUrl(DOMAIN, false, key);
        
        // 带有效期
        long expireInSeconds = 3600;
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String path;
        try {
            path = url.buildURL(auth, expireInSeconds);
            return path;
        } catch (QiniuException e) {
            System.err.println(e.code());
            System.err.println(e.response.toString());
            return null;
        }
    }
    
}
